<?php

namespace app\common\library\seo;

class Seo
{
    private static ?self $_instance = null;

    /** @var array<SeoInterface> */
    private array $seoInstance = [];


    /** @var array<string> */
    private array $errors = [];

    private function __construct(SeoInterface ...$seoInstance)
    {
        foreach ($seoInstance as $item) {
            $this->addSeoInstance($item);
        }
    }

    public static function instance(SeoInterface ...$seoInstance): self
    {
        if (is_null(self::$_instance)) {
            self::$_instance = new self(...$seoInstance);
        }
        return self::$_instance;
    }

    public function addSeoInstance(SeoInterface $seo)
    {
        $this->seoInstance[] = $seo;
    }

    public function post(array $urls): bool
    {
        foreach ($this->seoInstance as $instance) {
            if (!$instance->post($urls)) {
                $this->errors[] = sprintf("[%s]%s", get_class($instance), $instance->getMessage());
            }
        }
        return count($this->errors);
    }

    public function getError(): string
    {
        return join("\n", $this->errors);
    }
}